//
//  ZH_MineClassInfoViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/11/16.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_MineClassInfoViewController.h"
#import "ZH_MineViewModel.h"
#import "ZH_MineOrderTopTableViewCell.h"
#import "ZH_HomeCourceTableViewCell.h"
#import "ZH_HomeStateTableViewCell.h"
#import "ZH_MineClassStateTableViewCell.h"
@interface ZH_MineClassInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)ZH_MineViewModel *viewModel;
@end

@implementation ZH_MineClassInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    [self initData];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initUserInterface{
    self.view.backgroundColor = kFontColorWhite;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.view);
        make.left.equalTo(self.view).offset(FITSCALE(11));
        make.right.equalTo(self.view).offset(FITSCALE(-11));
    }];
}
- (void)initData{
    [self.viewModel.myCourseListParam setObject:self.statuString forKey:@"status"];
    WEAKSelf;
    [self.viewModel myCourseListResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            [weakSelf showHint:message];
        }
    }];
    
}
#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.viewModel.myCourseListArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    [array addObjectsFromArray:self.viewModel.myCourseListArray[section][@"list"]];
    return 2 + array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return FITSCALE(9);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    [array addObjectsFromArray:self.viewModel.myCourseListArray[indexPath.section][@"list"]];
    if (indexPath.row == 0) {
        ZH_MineOrderTopTableViewCell *cell = [ZH_MineOrderTopTableViewCell cellWithTableView:tableView];
        [cell updateUI:self.viewModel.myCourseListArray[indexPath.section]];
        return cell;
    }
    else if (indexPath.row == array.count + 1) {
        ZH_MineClassStateTableViewCell *cell = [ZH_MineClassStateTableViewCell cellWithTableView:tableView];
        [cell updataUI:self.viewModel.myCourseListArray[indexPath.section]];
        return cell;
    }
    else{
        ZH_HomeCourceTableViewCell *cell = [ZH_HomeCourceTableViewCell cellWithTableView:tableView];
        [cell updataClassUI:array[indexPath.row - 1]];
        [cell upclassUI:self.viewModel.myCourseListArray[indexPath.section]];
        return cell;
    }
    return nil;
}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_MineViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_MineViewModel alloc] init];
    }
    return _viewModel;
}
@end
